var cardpay = document.getElementsByClassName("cardpay")[0].getElementsByTagName("li");
var linepay = document.getElementsByClassName("linepay")[0].getElementsByTagName("li");

function clear() {
	for (var i = 0; i < cardpay.length; i++) {
		cardpay[i].removeAttribute("class");
	}
	for (var i = 0; i < linepay.length; i++) {
		linepay[i].removeAttribute("class");
	}
}

for (var i = 0; i < cardpay.length; i++) {
	cardpay[i].onclick = function() {
		clear();
		if (!this.hasAttribute("class")) {
			this.className += "active"
		} else {
			this.removeAttribute("class");

		}
	}
}

for (var i = 0; i < linepay.length; i++) {
	linepay[i].onclick = function() {
		clear();
		if (!this.hasAttribute("class")) {
			this.className += "active"
		} else {
			this.removeAttribute("class");

		}
	}
}

var pwd = document.getElementsByClassName("pay")[0].getElementsByTagName("input");
var reg_num = /^\d{1}$/;
for (var i = 0; i < pwd.length - 1; i++) {
	pwd[i].onclick = function() {
		if (pwd[0].value == "") {
			pwd[0].focus();
		}
		if (this.previousSibling.value == "") {
			for (var j = 0; j < pwd.length - 1; j++) {
				if (pwd[j].value != "") {
					pwd[j].nextSibling.focus();
				}
			}
		}
		if (pwd[5].value != "") {
			pwd[5].focus();
		}
	}

	pwd[i].onkeyup = function(e) {
		e = event || window.event;
		if (this.value.length == 1 && reg_num.test(this.value)) {
			this.nextSibling.focus();
		} else {
			this.value = "";
			this.focus();
		}
		if (pwd[5].value.length == 1 && reg_num.test(this.value)) {
			pwd[5].blur();
		}
		if (e.keyCode == 8) {
			pwd[5].focus();
			this.value = "";
			if (pwd[0].value == "") {
				pwd[0].focus();
			} else {
				this.previousSibling.focus();
			}
		}
	}
}

function subInput() {
	var pwd = document.getElementsByClassName("pay")[0].getElementsByTagName("input");
	for (var i = 0; i < pwd.length - 1; i++) {
		if (pwd[i].value == "") {
			alert("请输入支付密码！");
			if (pwd[0].value == "") {
				pwd[0].focus();
			}
			for (var j = 0; j < pwd.length - 1; j++) {
				if (pwd[j].value != "") {
					pwd[j].nextSibling.focus();
				}
			}
			break;
		}
	}
	if (pwd[0].value != "") {
		if (pwd[1].value != "") {
			if (pwd[2].value != "") {
				if (pwd[3].value != "") {
					if (pwd[4].value != "") {
						if (pwd[5].value != "") {
							if (confirm("是否支付?")) {
								window.location.href = "pay_success.html";
							} else {
								window.location.href = "pay_failure.html";
							}
						}
					}
				}
			}
		}
	}

}